<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button v-allow="'import_export'" type="warning" size="small">excel导入</el-button>
          <el-button v-allow="'export_export'" type="danger" size="small">excel导出</el-button>
          <el-button v-allow="'add'" type="primary" size="small" @click="hAdd">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template #default="{row}">
              <img
                v-imgerror="require('../../assets/common/bigUserHeader.png')"
                :src="row.staffPhoto"
                alt=""
                width="100%"
                @click="showCode(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ formatHireType(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small" @click="hAssign(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 5, 8, 10]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹层 -->
      <el-dialog title="新增角色" :visible.sync="showDialog">
        <addOrEdit @success="hSuccess" />
      </el-dialog>

      <!-- 分配角色弹层 -->
      <el-dialog title="分配角色" :visible.sync="roleDialog">
        <roleDialog v-if="roleDialog" :user-id="userId" @success="roleDialog=false" />
      </el-dialog>

      <!-- 二维码 -->
      <el-dialog title="二维码" :visible.sync="showCodeDialog">
        <el-row type="text" justify="center">
          <canvas ref="myCanvas" />
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import employeesEnum from '@/constant/employees'
import addOrEdit from './empDialog.vue'
import roleDialog from './roleDialog.vue'
import qrCode from 'qrcode'
// const hireType = {} // {1:正式,2:非正式}
// employeesEnum.hireType.forEach(item => {
//   hireType[item.id] = item.value
// })
const hireType = employeesEnum.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})
export default {
  components: {
    addOrEdit,
    roleDialog
  },
  data() {
    return {
      list: [],
      page: 1,
      size: 5,
      total: 0,
      showDialog: false,
      roleDialog: false,
      showCodeDialog: false,
      userId: ''
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    showCode(url) {
      this.showCodeDialog = true
      this.$nextTick(() => {
        qrCode.toCanvas(this.$refs.myCanvas, url)
      })
    },
    hAssign(id) {
      this.roleDialog = true
      this.userId = id
    },
    // 子传父 刷新页面
    hSuccess() {
      this.total++
      const maxPage = Math.ceil(this.total / this.size)
      this.page = maxPage
      this.loadEmployeeList()
      this.showDialog = false
    },
    hAdd() {
      this.showDialog = true
    },
    // 渲染页面
    async loadEmployeeList() {
      const res = await getEmployeeList(this.page, this.size)
      // console.log(res)
      this.list = res.data.data.rows
      this.total = res.data.data.total
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.page = 1
      this.size = val
      this.loadEmployeeList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployeeList()
    },
    formatHireType(value) {
      return hireType[value] || '未知'
    },
    // 删除角色
    hDel(id) {
      this.$confirm('你确定要删除该员工吗?').then(async() => {
        const res = await delEmployee(id)
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        if (res.data.code === 10000) {
          // 删除成功 刷新页面
          this.loadEmployeeList()
        }
      }).catch(error => console.log(error))
    }
  }
}
</script>

<style>

</style>
